package Core;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Alexander Naumow
 */
public class GUICanvas extends Canvas
{
	//public static int PU_KEY=1, PD_KEY=6, P_KEY=-8, PL_KEY=2, PR_KEY=5, LS_KEY=-6, RS_KEY=-7, BACK_KEY=7, POUND_KEY='#', STAR_KEY='*';
	//public static int PRESSED=0, RELEASED=1, REPEATED=2;

	public static FormManager manager;
	public static ImagePool pool = new ImagePool();
    public static GUICanvas inst;
	public static ClipMashine clipMashine;

	public GUICanvas()
	{
		setFullScreenMode(true);
        inst = this;
	}

	public void paint(Graphics g)
	{
		if(manager == null)
		{
			manager = new FormManager(getWidth(), getHeight());
			onGraphicsInited();
		}

		//long t = System.currentTimeMillis();

		g.setColor(0xffffff);
		g.fillRect(0, 0, getWidth(), getHeight());

		clipMashine = new ClipMashine(g);
		
		manager.draw(g, 0, 0);
	}

	protected void onGraphicsInited()
	{
	}

	protected void keyPressed(int keyCode)
	{
		manager.processKey(getNormalCode(keyCode), Key.PRESSED);
		repaint();
	}

	protected void keyReleased(int keyCode)
	{
		manager.processKey(getNormalCode(keyCode), Key.RELEASED);
		repaint();
		System.gc();
	}

	protected void keyRepeated(int keyCode)
	{
		manager.processKey(getNormalCode(keyCode), Key.REPEATED);
		repaint();
	}

	protected int getNormalCode(int i)
	{
		//System.out.println("getNormalCode input: "+i);

        int j = 0;
        String s1=""; String s2="";
        try{
            j = getGameAction(i);
        }catch(IllegalArgumentException _ex) { }

        try
        {
            s1 = getKeyName(i);
        }
        catch(IllegalArgumentException _ex)
        {
            s1 = null;
        }
        if(s1 != null)
        {
            //String s2;
            if((s2 = s1.toLowerCase()).equals("soft1") || s2.equals("Softkey 1") || s2.equals("softkey 1 ") || s2.equals("soft 1") || s2.equals("soft_1") || s2.equals("softkey 1") || s2.startsWith("left soft"))
                return Key.LS_KEY;
            if(s2.equals("soft2") || s2.equals("soft 2") || s2.equals("Softkey 4") || s2.equals("softkey 4 ") || s2.equals("soft_2") || s2.equals("softkey 4") || s2.startsWith("right soft"))
                return Key.RS_KEY;

	    if(j == Canvas.FIRE && i!=Canvas.KEY_NUM5){
            return Key.P_KEY;
        }else if(j == Canvas.LEFT && i!=Canvas.KEY_NUM4){
            return Key.PL_KEY;
        }else if(j == Canvas.RIGHT  && i!=Canvas.KEY_NUM6){
            return Key.PR_KEY;
        }else if(j == Canvas.UP  && i!=Canvas.KEY_NUM2){
            return Key.PU_KEY;
        }else if(j == Canvas.DOWN  && i!=Canvas.KEY_NUM8){
            return Key.PD_KEY;
        }

	    if(s2.equals("clear") || s2.equals("back")  || s2.equals("send"))
                return Key.BACK_KEY;
            if(s2.equals("select") || s2.equals("ok") || s2.equals("start") || s2.equals("Центральная клавиша") || s2.equals("Клавиша выбора") || s2.equals("клавиша выбора") || s2.equals("Центральная клавиша выбора") || s2.equals("Центральная клавиша выбора ") || s2.equals("центральная клавиша выбора") || s2.equals("центральная клавиша") || s2.equals("центральная клавиша выбора") || s2.equals("fire") || s2.equals("огонь!") || s2.equals("Огонь!") || s2.equals("navi-center") )
                return Key.P_KEY;
            if(s2.equals("up") || s2.equals("navi-up") || s2.equals("up arrow") || s2.equals("вверх") || s2.equals("Вверх") )
                return Key.PU_KEY;
            if(s2.equals("down") || s2.equals("navi-down") || s2.equals("down arrow") || s2.equals("вниз") || s2.equals("Вниз") )
                return Key.PD_KEY;
            if(s2.equals("left") || s2.equals("navi-left") || s2.equals("влево") || s2.equals("left arrow") || s2.equals("sideup"))
                return Key.PL_KEY;
            if(s2.equals("right") || s2.equals("navi-right") || s2.equals("вправо") || s2.equals("right arrow") || s2.equals("sidedown"))
                return Key.PR_KEY;
            if(s2.equals("q") || s2.equals("w"))
                return Key.LS_KEY;
            if(s2.equals("o") || s2.equals("p"))
                return Key.RS_KEY;
            if(s2.equals("escape"))
                return Key.BACK_KEY;
        }
        if(i == -6 || i == -21 || i == 21 || i == 105 || i == -202 || i == 112 || i == 57345)
            return Key.LS_KEY;
        if(i == -7 || i == -22 || i == 22 || i == 106 || i == -203 || i == 113 || i == 57346)
            return Key.RS_KEY;
        if(i == -5 || i == -10 || i == -20 || i == 20 || i == 23 || i == -14 || i == -26 || i == -200 || i == 10 || i == 13)
            return 8;
        if(i == -8 || i == -11 || i == -16 || i == -19 || i == -204)
            return Key.RS_KEY;

        switch(i){
        case 35: // '#'
        case 42: // '*'
        case 48: // '0'
        case 49: // '1'
        case 50: // '2'
        case 51: // '3'
        case 52: // '4'
        case 53: // '5'
        case 54: // '6'
        case 55: // '7'
        case 56: // '8'
        case 57: // '9'
            return i;
        }
		
        if(j != 0)
            return j;
        else
            return i;

    }
}
